//
//  FundDetailViewController.m
//  Umoney
//
//  Created by zwz on 16/8/13.
//  Copyright © 2016年 zhangwz. All rights reserved.
//

#import "FundDetailViewController.h"
#import "DangerViewController.h"


@interface FundDetailViewController ()<UITextFieldDelegate>
{
    NSString *PID;
    NSString *UID;
    NSArray *res;
    NSArray *inres;
}

@end

@implementation FundDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addGesture];
    // Do any additional setup after loading the view from its nib.
    self.pid.text = PID;
    
    self.ProductName.text =self.productname;
    float rate = [self.productrate floatValue];
    self.Rate.text = [NSString stringWithFormat:@"%.2f",rate];
    self.Rate2.text = [NSString stringWithFormat:@"%.2f",rate];
    self.Time.text = self.producttime;
    self.BeginMoney.text = self.productbegin;
    self.Danger.text = self.productdanger;
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(instancetype)initWithCode:(NSString *)code andUser:(NSString *)uid
{
    PID = code;
    UID = uid;
    _ready = @"no";
    
    //得到数据网络数据
    
    NSString *urlString = [NSString stringWithFormat:@"http://23.106.134.160/umoney/fund_list_detail.php?pid=%@",PID];
    
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    config.timeoutIntervalForRequest = 30.0f;
    config.timeoutIntervalForResource = 60.0f;
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
    NSURLSessionTask *task = [session dataTaskWithURL:[NSURL URLWithString:urlString] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        inres = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
  
        
    }];
    [task resume];
    
    while (!inres) {
    }
    for (NSDictionary *x in inres) {
        
        self.productname = [x valueForKey:@"pname"];
        
        float rate = [[x valueForKey:@"rate"] floatValue];
        self.productrate = [NSString stringWithFormat:@"%f",rate];
        
        self.producttime = [x valueForKey:@"totaltime"];
        
        self.productbegin = [x valueForKey:@"beginmoney"];
        
        self.productdanger = [x valueForKey:@"danger"];
        
        _ready = @"ready";
    }
    
    return self;
}

- (IBAction)DoInvest:(id)sender {
    if ([self.BeginMoney.text intValue] > [self.GetMoney.text intValue]) {
        [self Beginalter];
    }
    else
    {
        DangerViewController *risk;
        risk = [[DangerViewController alloc] initWithCode:PID andUser:UID getMoney:self.GetMoney.text];
        [self.navigationController pushViewController:risk animated:YES];
    }
    [self.GetMoney setText:@""];
}

-(void)Beginalter
{
    UIAlertController *altervc = [UIAlertController alertControllerWithTitle:@"提示" message:@"少于起投金额" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        
    }];
    [altervc addAction:confirm];
    
    [self presentViewController:altervc animated:NO completion:nil];
    
}

-(void)addGesture
{
    UITapGestureRecognizer *tap1=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewtaped:)];
    tap1.cancelsTouchesInView=NO;
    [self.view addGestureRecognizer:tap1];
}


-(void)viewtaped:(UIGestureRecognizer *)tap1
{
    [self.view endEditing:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)alter
{
    UIAlertController *altervc = [UIAlertController alertControllerWithTitle:@"提示" message:@"投资成功" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        [self.navigationController popToRootViewControllerAnimated:YES];
    }];
    [altervc addAction:confirm];
    
    [self presentViewController:altervc animated:NO completion:nil];
    
}

-(void)erroralter
{
    UIAlertController *altervc = [UIAlertController alertControllerWithTitle:@"提示" message:@"投资失败，网络错误" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        [self.navigationController popToRootViewControllerAnimated:YES];
    }];
    [altervc addAction:confirm];
    
    [self presentViewController:altervc animated:NO completion:nil];
    
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
